<?php
add_action('widgets_init', 'register_slider_event_widget'); // load widget

function register_slider_event_widget() { // register widget
    register_widget('slider_event');
}

class slider_event extends WP_Widget {// widget class {

    function slider_event() { 
    // widget setting | class name must be same function name
        $widget_ops = array('classname' => 'Lastest news-w', 'description' =>
            'Display slider event');
        $control_ops = array(
            'width' => 250,
            'height' => 350,
            'id_base' => 'slider_event');
        $this->WP_Widget('slider_event', 'CouponX - Event slider event page', $widget_ops, $control_ops);
    }

    function widget($args, $instance) {
        global $wpdb,$table_prefix;
        $countItem = $instance['count_item'];
        if($countItem == ''){
            $countItem = 5;
        }
        $event_id = $instance['event'];
        $term = get_term_by('id', $event_id, 'event');
        $my_query="
            SELECT SQL_CALC_FOUND_ROWS ".$table_prefix."posts.ID ,".$table_prefix."posts.post_content,".$table_prefix."posts.post_title FROM ".$table_prefix."posts 
            INNER JOIN ".$table_prefix."postmeta ON (".$table_prefix."posts.ID = ".$table_prefix."postmeta.post_id) 
            INNER JOIN ".$table_prefix."term_relationships ON (".$table_prefix."posts.ID = ".$table_prefix."term_relationships.object_id) 
            WHERE 1=1 AND ( ".$table_prefix."term_relationships.term_taxonomy_id IN ($event_id)) 
            AND ".$table_prefix."posts.ID = ".$table_prefix."postmeta.post_id 
            AND ".$table_prefix."posts.post_type = 'coupon' AND (".$table_prefix."posts.post_status = 'publish') 
            AND ".$table_prefix."postmeta.meta_key = 'store_coupon_metadata' 
            GROUP BY ".$table_prefix."postmeta.meta_value ORDER BY ".$table_prefix."posts.post_date DESC LIMIT 0,$countItem
        ";
        $coupons = $wpdb->get_results($my_query);
        if(is_tax('event',array($term->slug))){
      ?>
                        <div id="da-slider" class="row-fluid da-slider">
                            <?php 
                                foreach ($coupons as $cp){
                                    $cp_id = $cp->ID;
                                    $cp_name = $cp->post_title;
                                    $store_id = get_post_meta($cp_id, 'store_coupon_metadata', true);
                                    $store_name = get_the_title($store_id);
                                    $outlink_cp = home_url().'/'.get_option('aff_out_link_prefix_cp').'/'.$cp_id;
                            ?>
				<div id="<?php echo $cp_id;?>" class="da-slide data-cp" data-store="<?php echo $store_id;?>" data-current_url="<?php echo cpx_current_url();?>">
                                    <p class="store-name"><a href="<?php echo get_permalink($store_id);?>" title="<?php echo 'go to '.$store_name;?>"><?php echo $store_name;?></a></p>
                                    <p class="title coupon_title">
                                        <a rel="nofollow" href="<?php echo $outlink_cp;?>" target="<?php echo $store_id;?>"
                                           class="coupon_title title" title="<?php echo $cp_name;?>">
                                                <?php echo $cp_name;?>
                                        </a>
                                    </p>
                                    <p class="content">
                                            <?php echo $cp->post_content;?>
                                    </p>
                                        <div class="da-img">
                                                <img src="<?php echo get_post_meta($store_id,'logo_metadata',true);?>" alt="<?php echo $store_name;?>" />
                                        </div>
				</div>
                            <?php 
                                }
                            ?>
				<nav class="da-arrows">
					<span class="da-arrows-prev"></span>
					<span class="da-arrows-next"></span>
				</nav>
			</div>
    <?php }
        }

    function update($new_instance, $old_instance) { // update widget
        $instance = $old_instance;
        $instance['count_item'] = $new_instance['count_item'];
        $instance['event'] = $new_instance['event'];
        return $instance;
    }

    function form($instance) { // form for the widget options
        $event = get_terms('event',array('hide_empty'=> false));
    ?>
        <div style="color: #333;">
            <p>
    		<label for="<?php echo $this->get_field_id('count_item');?>"><?php echo 'Enter number coupon display ';?></label>
    		<input type="text" id="<?php echo $this->get_field_id('count_item');?>" name="<?php echo $this->get_field_name('count_item');?>" value="<?php echo $instance['count_item'];?>" style="width:90%;" />
            </p>
            <select name="<?php echo $this->get_field_name('event'); ?>" id="<?php echo $this->get_field_id('event') ?>" style="width: 80%;">
                <?php
                foreach ($event as $v) {
                    if ($instance['event'] == $v->term_id)
                        $selected = 'selected';
                    else
                        $selected = '';
               ?>
                    <option value="<?php echo $v->term_id;?>" <?php echo $selected;?> ><?php echo $v->name;?></option>
                <?php }
                ?>
            </select>
        </div>
        <?php
    }

}
?>
